
package cn.zero.letaomallspringboot.service.impl;

import cn.zero.letaomallspringboot.common.PageResult;
import cn.zero.letaomallspringboot.entity.GoodCategory;
import cn.zero.letaomallspringboot.mapper.GoodCategoryMapper;
import cn.zero.letaomallspringboot.service.IGoodCategoryService;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GoodCategoryService implements IGoodCategoryService {

    private final GoodCategoryMapper goodCategoryMapper;

    public GoodCategoryService(GoodCategoryMapper goodCategoryMapper) {
        this.goodCategoryMapper = goodCategoryMapper;
    }

    public List<GoodCategory> listAll() {
        return goodCategoryMapper.listAll();
    }

    public PageResult<GoodCategory> listByPage(Integer page, Integer size) {
        Integer offset = (page - 1) * size;
        List<GoodCategory> list = goodCategoryMapper.listByPage(offset, size);
        Long total = goodCategoryMapper.countTotal();
        return new PageResult<>(page, size, total, list);
    }

    public GoodCategory getById(Integer id) {
        return goodCategoryMapper.getById(id);
    }

    public boolean deleteById(Integer id) {
        return goodCategoryMapper.deleteById(id) > 0;
    }

    public boolean insert(GoodCategory category) {
        return goodCategoryMapper.insert(category) > 0;
    }

    public boolean update(GoodCategory category) {
        category.setUpdateTime(new Date());
        return goodCategoryMapper.update(category) > 0;
    }

    //TODO: update_time为null
    public boolean updateStatus(Integer id, Integer status) {
        GoodCategory category = new GoodCategory();
        category.setId(id);
        category.setStatus(status);
        category.setUpdateTime(new Date());
        return goodCategoryMapper.updateStatus(category) > 0;
    }
}